package com.eastfair.common.controller.authcode;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.common.dto.AuthCodeUserDTO;
import com.eastfair.common.dto.AuthCodeUserPageQuery;
import com.eastfair.common.dto.AuthCodeUserSaveDTO;
import com.eastfair.common.dto.AuthCodeUserUpdateDTO;
import com.eastfair.common.entity.AuthCodeUser;
import com.eastfair.common.service.AuthCodeUserService;
import com.eastfair.common.vo.AuthCodeUserVO;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 服务商对外授权码与用户关系
 * </p>
 *
 * @author dqq
 * @date 2023-02-14
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/authCodeUser")
@Api(value = "AuthCodeUser", tags = "服务商对外授权码与用户关系")
@PreAuth(replace = "common:authCodeUser:")
public class AuthCodeUserController extends SuperController<AuthCodeUserService, Long, AuthCodeUser, AuthCodeUserPageQuery, AuthCodeUserSaveDTO, AuthCodeUserUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private AuthCodeUserService authCodeUserService;

    @Override
    public void handlerResult(IPage<AuthCodeUser> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<AuthCodeUser> authCodeUserList = list
                .stream()
                .map((map) -> AuthCodeUser.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(authCodeUserList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<AuthCodeUserPageQuery> params) {
        AuthCodeUserPageQuery pageQuery = params.getModel();
    }

    /**
     * 授权码查询授权用户列表.
     *
     * @param authCode the auth code
     * @return the r
     */
    @GetMapping("/query/by/{authCode}")
    public R<List<AuthCodeUserVO>> queryByAuthCode(@PathVariable("authCode") String authCode) {
        log.info("queryByAuthCode - 授权码查询授权用户列表, authCode={}", authCode);
        return R.successDef(authCodeUserService.listVOByAuthCode(authCode));
    }

    /**
     * 外部用户绑定授权码.
     *
     * @param authCodeUserDTO the auth code user dto
     * @return the r
     */
    @PostMapping("/bind")
    public R<Boolean> bindAuthCodeUser(@RequestBody AuthCodeUserDTO authCodeUserDTO) {
        log.info("bindAuthCodeUser - 外部用户绑定授权码, authCodeUserDTO={}", authCodeUserDTO);
        return authCodeUserService.bindAuthCodeUser(authCodeUserDTO);
    }
}
